package com.rzdata.poct.system.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.rzdata.poct.common.utils.DateUtils;
import com.rzdata.poct.system.domain.business.bo.HomeBO;
import com.rzdata.poct.system.domain.business.vo.IndicatorVO;
import com.rzdata.poct.system.domain.business.vo.ProjectEchartVO;
import com.rzdata.poct.system.domain.business.vo.QualityEchartVO;
import com.rzdata.poct.system.domain.business.vo.QualityVO;
import com.rzdata.poct.system.mapper.HomePageMapper;
import com.rzdata.poct.system.service.IHomePageService;
import io.jsonwebtoken.lang.Objects;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @auther xcy
 * @create 2021-09-17 8:51
 */
@Service
public class HomePageServiceImpl implements IHomePageService {

    @Autowired
    HomePageMapper homePageMapper;

    @Override
    public List<IndicatorVO> selectIndicatorList(HomeBO bo) {
        List<IndicatorVO> list = new ArrayList<>();
        // 当前时间
        Date nowSTime = ObjectUtil.isNotEmpty(bo.getStartTime()) ? bo.getStartTime() : new Date();
        Date nowETime = ObjectUtil.isNotEmpty(bo.getEndTime()) ? bo.getEndTime() : new Date();
        // 对比时间
        Date lastSTime = DateUtils.getSpecifiedDayBefore(nowSTime, 2, 1);
        Date lastETime = DateUtils.getSpecifiedDayBefore(nowETime, 2, 1);
        // 项目个数
        double nowProNum = homePageMapper.getProjectCount(bo.getDeptId(), nowSTime, nowETime);
        double lastProNum = homePageMapper.getProjectCount(bo.getDeptId(), lastSTime, lastETime);
        IndicatorVO pro = handleData(nowProNum, lastProNum);
        pro.setName("质控项目个数");
        list.add(pro);

        // 仪器个数
        double nowInsNum = homePageMapper.getInsCount(bo.getDeptId(), nowSTime, nowETime);
        double lastInsNum = homePageMapper.getInsCount(bo.getDeptId(), lastSTime, lastETime);
        IndicatorVO ins = handleData(nowInsNum, lastInsNum);
        ins.setName("检测仪器个数");
        list.add(ins);

        // 检测样本个数
        double nowSamNum = homePageMapper.getSampleCount(nowSTime, nowETime);
        double lastSamNum = homePageMapper.getSampleCount(lastSTime, lastETime);
        IndicatorVO sam = handleData(nowSamNum, lastSamNum);
        sam.setName("检测样本个数");
        list.add(sam);

        //危急值预警个数
        double nowCriNum = homePageMapper.getCrisisCount(bo.getDeptId(), nowSTime, nowETime);
        double lastCriNum = homePageMapper.getCrisisCount(bo.getDeptId(), lastSTime, lastETime);
        IndicatorVO cri = handleData(nowCriNum, lastCriNum);
        cri.setName("危急值预警个数");
        list.add(cri);

        // 在控占比
        List<QualityVO> nowList = homePageMapper.getOutCount(bo.getDeptId(), nowSTime, nowETime);
        List<QualityVO> lastList = homePageMapper.getOutCount(bo.getDeptId(), nowSTime, nowETime);
        double nowOutNum = 0.0;
        double lastOutNum = 0.0;
        if (ObjectUtil.isNotEmpty(nowList)) {
            nowOutNum = nowList.stream().filter(x -> x.getQualityStatus().equals(1)).collect(Collectors.toList()).size();
        }
        if (ObjectUtil.isNotEmpty(lastList)) {
            lastOutNum = lastList.stream().filter(x -> x.getQualityStatus().equals(1)).collect(Collectors.toList()).size();
        }
        IndicatorVO out = handleData(nowOutNum, lastOutNum);
        out.setNumber(0+"%");
        if (nowOutNum > 0) {
            double oo = nowOutNum / nowList.size();
            BigDecimal outNum = new BigDecimal(oo * 100).setScale(2, RoundingMode.HALF_UP);
            out.setNumber(outNum + "%");
        }
        out.setName("质控在控占比");
        list.add(out);

        return list;
    }

    private IndicatorVO handleData(double nowNum, double lastNum) {
        IndicatorVO indicatorVO = new IndicatorVO();
        //同比增长率=（本期数－同期数）÷同期数×100%
        //环比增长率=（本期数-上期数）/上期数×100%。
        BigDecimal proOnRate = BigDecimal.ZERO;
        if (nowNum > 0) {
            double on = (nowNum - lastNum) / nowNum;
            proOnRate = new BigDecimal(on * 100).setScale(2, RoundingMode.HALF_UP);
        }
        BigDecimal proLinkRate = BigDecimal.ZERO;
        if (lastNum > 0) {
            double link = (nowNum - lastNum) / lastNum;
            proLinkRate = new BigDecimal(link * 100).setScale(2, RoundingMode.HALF_UP);
        }
        indicatorVO.setNumber(String.format("%.0f", nowNum));
        indicatorVO.setOnRate(proOnRate);
        indicatorVO.setLinkRate(proLinkRate);
        indicatorVO.setOnType(0);
        indicatorVO.setLinkType(0);
        if (proOnRate.compareTo(BigDecimal.ZERO) > -1) {
            indicatorVO.setOnType(1);
        }
        if (proLinkRate.compareTo(BigDecimal.ZERO) > -1) {
            indicatorVO.setLinkType(1);
        }
        return indicatorVO;
    }

    @Override
    public List<QualityEchartVO> selectQualityEchart(HomeBO bo) {
        return homePageMapper.getQualityEchart(bo.getDeptId(), bo.getStartTime(), bo.getEndTime());
    }

    @Override
    public List<ProjectEchartVO> selectProjectEchart(HomeBO bo){
        return homePageMapper.selectProjectEchart(bo);
    }
}
